//
//  HWHomePageSearchViewController.m
//  HappyWeekend
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWHomePageSearchViewController.h"
#import "HWSearchMenuView.h"
#import "HWSearchCollectionViewCell.h"
#import "HWHomePageMainListTableViewCell.h"
#import "HWSearchContentModel.h"
#import "HWHomePageMainModel.h"

@interface HWHomePageSearchViewController ()<UISearchBarDelegate, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UICollectionView *productCollectionView;

@property (nonatomic, strong) UITableView *topicTableView;

@property (nonatomic, strong) NSMutableArray *tableViewArray;

@property (nonatomic, strong) NSMutableArray *collectionViewArray;


@end

@implementation HWHomePageSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    
    [self addSearchController];
    
    
    HWSearchMenuView *searchMenuView = [[HWSearchMenuView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 30)];
    searchMenuView.backgroundColor = [UIColor whiteColor];
    [searchMenuView.productButton setTitle:@"商品" forState:(UIControlStateNormal)];
    [searchMenuView.productButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [searchMenuView.topicButton setTitle:@"专题" forState:(UIControlStateNormal)];
    [searchMenuView.topicButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.view addSubview:searchMenuView];
    
    [self addScrollView];
    
    [self addTableView];
    
    [self addCollectionView];
    
}


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSString *str = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [HWRequestManager requestWithUrl:[NSString stringWithFormat:@"http://api.guozhoumoapp.com/v2/search/item?keyword=%@&sort=&offset=0&limit=20",str] parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *itemsArray = dic[@"data"][@"items"];
        self.collectionViewArray = [NSMutableArray array];
        for (NSDictionary *aDic in itemsArray) {
            HWSearchContentModel *model = [[HWSearchContentModel alloc] init];
            [model setValuesForKeysWithDictionary:aDic];
            [self.collectionViewArray addObject:model];
        }
        [self.productCollectionView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
    
    
    [HWRequestManager requestWithUrl:[NSString stringWithFormat:@"http://api.guozhoumoapp.com/v2/search/post?keyword=%@&sort=&offset=0&limit=20",str] parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *postsArray = dic[@"data"][@"posts"];
        self.tableViewArray = [NSMutableArray array];
        for (NSDictionary *aDic in postsArray) {
            HWHomePageMainModel *model = [[HWHomePageMainModel alloc] init];
            [model setValuesForKeysWithDictionary:aDic];
            [self.tableViewArray addObject:model];
        }
        [self.topicTableView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
    
}


- (void)addScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, kScreenWidth, kScreenHeight - 30 - 64)];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * 2, kScreenHeight - 94);
    
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    
}

- (void)addCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(160, 558 * 160 / 521 + 80);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.productCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 30 - 64 - 49) collectionViewLayout:layout];
    self.productCollectionView.dataSource = self;
    self.productCollectionView.delegate = self;
    self.productCollectionView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:self.productCollectionView];
    
    [self.productCollectionView registerClass:[HWSearchCollectionViewCell class] forCellWithReuseIdentifier:@"HWSearchCollectionViewCell"];
}


- (void)addTableView
{
    self.topicTableView = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 64 - 30 - 49) style:(UITableViewStylePlain)];
    _topicTableView.dataSource = self;
    _topicTableView.delegate = self;
    _topicTableView.rowHeight = 30 + kScreenWidth / 600 * 275;
    _topicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scrollView addSubview:_topicTableView];
    [_topicTableView registerClass:[HWHomePageMainListTableViewCell class] forCellReuseIdentifier:@"HWHomePageMainListTableViewCell"];
    
}





- (void)addSearchController
{
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchBar.delegate = self;
    self.searchController.searchBar.showsCancelButton = YES;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 64)];
    [view addSubview:self.searchController.searchBar];
    [self.view addSubview:view];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.navigationController.navigationBarHidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWHomePageMainListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageMainListTableViewCell"];
    cell.timeLabel.hidden = YES;
    HWHomePageMainModel *model = self.tableViewArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionViewArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HWSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HWSearchCollectionViewCell" forIndexPath:indexPath];
    HWSearchContentModel *model = self.collectionViewArray[indexPath.row];
    cell.model = model;
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
